import React from 'react';
import { connect } from 'dva';
import { Button, Table, Form, Input, Popconfirm,DatePicker, Select,TreeSelect,   } from 'antd';
const FormItem = Form.Item;
const Option = Select.Option;
const TreeNode = TreeSelect.TreeNode;
const RangePicker = DatePicker.RangePicker;

import AddInventoryModal from '../../components/inventory/AddInventoryModal';
import ViewSheetModal from '../../components/inventory/ViewSheetModal';
import ViewInventoryModal from '../../components/inventory/ViewInventoryModal';

function Inventory ({ dispatch, inventory, loading, form }) {

    const { getFieldDecorator, getFieldsValue, getFieldValue } = form;
    const { currentItem, modalVisible, modalType,roomList,newKey,userList, rackList,isShowUser,currentViewItem,modalViewVisible,detailList,cycletime} = inventory

    const columns = [{
        title: '计划名称',
        dataIndex: 'inventoryName',
        key: 'inventoryName',
        width: 100,
    },{
        title: '机房',
        dataIndex: 'roomName',
        key: 'roomName',
        width: 100,
    },{
        title: '盘点类型',
        dataIndex: 'inventoryType',
        key: 'inventoryType',
        width: 80,
        render: (value, row, index) => {
            return (
                value==1?"自动盘点":value==2?"手动盘点":""
            )
        }
    },{
        title: '重复周期',
        dataIndex: 'cycleName',
        key: 'cycleName',
        width: 80,
    },{
        title: '盘点时间',
        dataIndex: 'inventoryTime',
        key: 'inventoryTime',
        width: 150,
    },{
        title: '盘点人',
        dataIndex: 'operatiorName',
        key: 'operatiorName',
        width: 100,
    },{
        title: '备注',
        dataIndex: 'remark',
        key: 'remark',
        width: 120,
    },{
        title: '状态',
        dataIndex: 'status',
        key: 'status',
        width: 80,
        render: (value, row, index) => {
            return (
                value==1?"未处理":value==2?"执行中":value==3?"已完成":""
            )
        }
    },{
        title: '操作',
        key: 'operation',
        width: 80,
        render: (text, record) => (
            <span>
               {(record.status==1)?(
                <span>
                    <a onClick={onEditItem.bind(null, record)}>编辑</a>
                    <span className="ant-divider" />
                    <Popconfirm title="确认删除？" onConfirm={deleteHandler.bind(null, record.id_)}>
                    <a>删除</a>
                    </Popconfirm>
                </span>
               ):(record.status==2)?(
                <span>
                    <Popconfirm title="确认删除？" onConfirm={deleteHandler.bind(null, record.id_)}>
                    <a>删除</a>
                    </Popconfirm>
                    <span className="ant-divider" />
                    <span>
                      <a onClick={onViewSheet.bind(null, record.id_,record.inventoryName)}>查看盘点单</a>
                    </span>
                </span>
               ):<span>
                   <a onClick={onViewSheet.bind(null, record.id_,record.inventoryName)}>查看盘点单</a>
                </span> }
            </span>
        ),
    }];

    const pagination = {
        current:inventory.current,
        pageSize:inventory.currentSize,
        total: inventory.total,
        showSizeChanger: true,
        showTotal: (total => '共' + inventory.total + '条'),
        onShowSizeChange(current, size) {
            dispatch({
                type: 'inventory/queryList',
                payload: { ...getFieldsValue(),...{'pageNum':current, 'pageSize':size} },
            });
        },
        onChange(current) {
            dispatch({
                type: 'inventory/queryList',
                payload: { ...getFieldsValue(),...{'pageNum':current, 'pageSize':inventory.currentSize} },
            });
        },
    };
    const editHandler = (id, values) => {
        dispatch({
            type: 'inventory/edit',
            payload: { id, ...values },
        });
    }
    const onEditItem = (record) => {
        dispatch({
        type: 'inventory/qryById',
        payload: record,
        })
    }
    const submitHandle = () => {
        dispatch({
            type: 'inventory/queryList',
            payload: { ...getFieldsValue() },
        });
    }
    function handleClear(){
      form.resetFields();
    }
    const onViewItem = (id) => {
        dispatch({
            type: 'inventory/onViewItem',
            payload:id,
        })
    }

    //查看盘点单
    const onViewSheet = (id,name) => {
        dispatch({
            type: 'inventory/onViewSheet',
            payload:{inventoryId:id},
            name :name
        })
    }
    const addHandler = () => {
        dispatch({
        type: 'inventory/showModal',
        payload: {
            modalType: 'create',
            isShowUser:2
        },
        })
    }
    function deleteHandler(id) {
        dispatch({
        type: 'inventory/remove',
        payload: {id:id,search:{...getFieldsValue()}},
        });
    }
    //下达盘点单
    function onGoInventory(id) {
        dispatch({
        type: 'inventory/goInventory',
        payload: {id:id,search:{...getFieldsValue()}},
        });
    }

    const modalProps = {
        item: modalType === 'create' ? {} : currentItem,
        roomList: roomList,
        userList:userList,
        rackList:rackList,
        cycletime:cycletime,
        visible: modalVisible,
        isShowUser:isShowUser,
        type: modalType,
        dispatch:dispatch,
        maskClosable: false,
        confirmLoading: loading,
        title: `${modalType === 'create' ? '新增盘点' : '编辑盘点'}`,
        wrapClassName: 'vertical-center-modal',
        newKey : newKey,
        onOk (data) {
            dispatch({
                type: `inventory/${modalType}`,
                payload: data,
            })
        },
        onCancel () {
            dispatch({
                type: 'inventory/hideModal',
            })
        },
        onRadioChange (data) {
            dispatch({
                type: 'inventory/onRadioChange',
                payload: data,
            })
        },
        handleRoomChange (data) {
            dispatch({
                type: 'inventory/qryRackList',
                payload: data,
            })
        },
    }

    const modalViewProps = {
        item: currentViewItem,
        visible: modalViewVisible,
        detailList:detailList,
        dispatch:dispatch,
        assetCurrent:inventory.assetCurrent,
        assetCurrentSize:inventory.assetCurrentSize,
        assetTotal:inventory.assetTotal,
        maskClosable: false,
        confirmLoading: loading,
        title: inventory.viewModalType === 'view' ? <div style={{fontSize:'16px'}}><span>盘点名称:{currentViewItem.inventorySheetName}</span></div>:'',
        wrapClassName: 'vertical-center-modal',
        newKey : newKey,
        onCancel () {
            dispatch({
                type: 'inventory/hideViewModal',
            })
        },
    }

    const modalViewSheetProps = {
        visible: inventory.modalViewSheetVisible,
        listSheet:inventory.listSheet,
        inventoryId:inventory.inventoryId,
        dispatch:dispatch,
        current:inventory.currentSheet,
        currentSize:inventory.currentSizeSheet,
        total:inventory.totalSheet,
        maskClosable: false,
        confirmLoading: loading,
        title: '盘点计划：'+inventory.inventoryName+" 下的盘点单",
        wrapClassName: 'vertical-center-modal',
        newKey : newKey,
        onCancel () {
            dispatch({
                type: 'inventory/hideViewSheetModal',
            })
        },
    }

    const loopRoom = data => data.map((item) => {
      return <Option key={item.id_ + ''}>{item.name}</Option>
    });

    return (
         <div style={{backgroundColor:'#fff'}}>
          <div style={{minHeight:'100%'}}>
              <div style={{width:'100%',backgroundColor:'#fff',lineHeight:'40px',paddingLeft:'20px',paddingTop:'10px'}}>
                  <Form layout="inline" >
                    <FormItem>
                        {
                            getFieldDecorator('roomId')(
                             <Select
                                  showSearch
                                  style={{ width: 250 }}
                                  placeholder="选择机房"
                                  optionFilterProp="children"
                              >
                              {loopRoom(inventory.roomList)}
                              </Select>
                            )
                         }
                    </FormItem>
                    <FormItem>
                            {getFieldDecorator('inventoryType')(
                            <Select
                                showSearch
                                style={{ width: 180 }}
                                placeholder="选择盘点类型"
                                optionFilterProp="children"
                        >
                              <Option value="">全部</Option>
                              <Option value="1">自动盘点</Option>
                              <Option value="2">手动盘点</Option>
                            </Select>
                        )}
                    </FormItem>                  
                    <FormItem>
                        {getFieldDecorator('status')(
                            <Select
                                showSearch
                                style={{ width: 150 }}
                                placeholder="选择状态"
                                optionFilterProp="children"
                        >
                              <Option value="">全部</Option>
                              <Option value="1">未处理</Option>
                              <Option value="2">执行中</Option>
                              {/*<Option value="3">已完成</Option>*/}
                            </Select>
                        )}
                    </FormItem>

                    {/*<FormItem>
                           {getFieldDecorator('beginTime')(
                                <RangePicker style={{ width:'300px' }}  format="YYYY-MM-DD" />
                            )}
                    </FormItem>*/}

                    <FormItem>
                        <Button type="primary" size='large' onClick={submitHandle} className='ant-search-btn'>查询</Button>
                    </FormItem>
                    <FormItem>
                        <Button size="large" htmlType="reset" onClick={handleClear}>清空</Button>
                    </FormItem>

                    <div style={{marginTop:'-40px',textAlign:'right',paddingRight:'10px'}}>
                        <FormItem>
                            <Button type="primary" size='large' onClick={addHandler}>制定计划</Button>
                        </FormItem>
                    </div>
                </Form>
            </div>

            <div style={{width:'100%',padding:"30px 20px",}}>
                <Table columns={columns} rowKey={record => record.id_} loading={loading} dataSource={inventory.list} pagination={pagination}
                scroll={{ y: document.body.clientHeight - 218 }}
                />
            </div>
            </div>
                <AddInventoryModal {...modalProps} />
                <ViewSheetModal {...modalViewSheetProps} />
                <ViewInventoryModal {...modalViewProps} />
        </div>
    );
}

function mapStateToProps(state) {
  return { inventory : state.inventory,loading:state.loading.models.inventory};
}

Inventory = Form.create()(Inventory);
export default connect(mapStateToProps)(Inventory);
